package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomFacilityService roomFacilityService;//房间和配套相关信息
    @Autowired
    private RoomLabelService roomLabelService;//房间和标签相关信息
    @Autowired
    private RoomAttrValueService roomAttrValueService;//房间和属性值相关信息
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;//房间和支付方式相关信息
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;//房间和租期相关信息
    @Autowired
    private GraphInfoService graphInfoService;//房间和图片相关信息
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;


    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        //判断当前功能是更新或新增
        boolean isUpdate = roomSubmitVo.getId() != null && roomSubmitVo.getId() != 0;
        //更新或新增房间信息
        this.saveOrUpdate(roomSubmitVo);
        //获取房间Id
        Long roomId = roomSubmitVo.getId();
        //若当前功能为更新功能，则先删除房间对应的配套，标签，属性，支付方式，租期，图片相关信息
        if(isUpdate) {
            //应先判断更新的id值存不存在，不存在则抛出异常

            //1、删除房间对应的配套关系
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomId);
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

            //2、删除房间对应的标签关系
            LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomId);
            roomLabelService.remove(labelLambdaQueryWrapper);

            //3、删除房间对应的属性关系
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            //4、删除房间对应的支付方式关系
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomId);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            //5、删除房间对应的租期关系
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

            //6、删除房间对应的图片相关信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, roomId);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }

        //添加房间对应的配套，标签，属性，支付方式，租期，图片相关信息
        //添加房间和配套的关系
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilities = new ArrayList<>();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomId).facilityId(facilityInfoId).build();
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }

        //添加房间和标签的关系
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabels = new ArrayList<>();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomId).labelId(labelInfoId).build();
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }

        //添加房间和属性值的关系
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValues = new ArrayList<>();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomId).attrValueId(attrValueId).build();
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }


        //添加房间和支付方式的关系
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomId).paymentTypeId(paymentTypeId).build();
                roomPaymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }

        //添加房间和租期的关系
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomId).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }


        //添加房间和图片的关系
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos = new ArrayList<>();
        if(!CollectionUtils.isEmpty(graphVoList)){
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomId);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
    }

    @Override
    public IPage<RoomItemVo> findRoomItemVoPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.findRoomItemVoPage(page,queryVo);
    }

    @Override
    public RoomDetailVo findRoomDetailVoById(Long roomId) {
        //1、根据房间id查找房间相关信息
        RoomInfo roomInfo = this.getById(roomId);
        //2、根据房间id查找所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.getApartmentInfoById(roomInfo.getApartmentId());
        //3、根据房间id查找图片列表
        List<GraphVo> graphVoList = graphInfoMapper.findGraphVoListByItemTypeAndItemId(ItemType.ROOM,roomInfo.getId());
        //4、根据房间id查找属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.findAttrValueVoListByRoomId(roomId);
        //5、根据房间id查找配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findFacilityInfoListByRoomId(roomId);
        //6、根据房间id查找标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.findLabelInfoListByRoomId(roomId);
        //7、根据房间id查找支付方式列表
        List<PaymentType> paymentTypes = paymentTypeMapper.findPaymentTypeListByRoomId(roomId);
        //8、根据房间id查找可选租期列表
        List<LeaseTerm> leaseTermslist = leaseTermMapper.findLeaseTermListByRoomId(roomId);
        //创建RoomDetailVo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //属性赋值
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setLeaseTermList(leaseTermslist);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        return roomDetailVo;

    }

    @Override
    public void removeRoomInfo(Long roomId) {
        //1、删除房间
        this.removeById(roomId);

        //2、删除房间和属性相关信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomId);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //3、删除房间和配套相关信息
        LambdaQueryWrapper<RoomFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomId);
        roomFacilityService.remove(facilityInfoLambdaQueryWrapper);

        //4、删除房间和标签相关信息
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomId);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        //5、删除房间和租期相关信息
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

        //6、删除房间和支付方式相关信息
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomId);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //7、删除房间和图片相关信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM).eq(GraphInfo::getItemId,roomId);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

    }
}




